<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class PropLog extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'prop_log';
    protected $table_log = 'send_prop_log';

    /**
     * 道具赠送
     * @param $data
     * @return mixed
     */
    public function sendProp($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/prop/grant', $data, 'urlencoded', 'GET');
    }

    public function propLog($opt_id, $opt_name, $room_id, $prop_id, $num)
    {
        $data['opt_id'] = $opt_id;
        $data['opt_name'] = $opt_name;
        $data['room_id'] = $room_id;
        $data['prop_id'] = $prop_id;
        $data['num'] = $num;
        $data['created_at'] = date('Y-m-d H:i:s', time());
        return DB::table($this->table_log)->insert($data);
    }

    public function getList($input = [])
    {
        $limit = $input['limit'] ?? 10;
        $where = '  ';
        $room_uid = $input['room_uid'] ?? '';
        if ($room_uid) {
            $where .= ' WHERE room_uid = ' . $input['room_uid'];
        }
        $sql = "select s.*,room_uid,p.name as pname , r.name as rname from cms_send_prop_log as s 
                    left join `hypd`.zb_room as r on r.room_id = s.room_id  
                    left join `hypd`.zb_prop as p on p.prop_id = s.prop_id
                    $where order by s.created_at desc";
        return $this->objToArray(DB::table(Db::raw("($sql) as aaaaa"))->paginate($limit));
    }

    public function getPropLog($user_id, $start_time, $end_time, $type = 5)
    {
        $where['user_id'] = $user_id;
        $where['type'] = $type;
        $whereIn = [46, 49];
        $whereDate = [$start_time, $end_time];
        $res = DB::connection($this->connection)->table($this->table)->whereIn('prop_id',$whereIn)->whereBetween('created_at',$whereDate)->where($where)->get();
        return $this->objToArray($res);

    }

    public function getPropLogList($input)
    {
        $limit = $input['limit'] ?? 30;
        $uuid = $input['uuid'];
        $gift_id = $input['gift_id'];
        $begin_date = $input['begin_date'] != '' ? strtotime($input['begin_date']) : '';
        $end_date = $input['end_date'] != '' ? strtotime($input['end_date']) : '';


        $where = [];
        if($uuid != '') $where[] = ['uuid', '=', "{$uuid}"];
        if($gift_id != '') $where[] = [$this->table.'.prop_id', '=', $gift_id];
        if($begin_date && $end_date) {
            $where[] = [$this->table.'.created_at', '>=', $begin_date];
            $where[] = [$this->table.'.created_at', '<=', $end_date];
        }
        if($begin_date && !$end_date) $where[] = [$this->table.'.created_at', '>=', $begin_date];
        if(!$begin_date && $end_date) $where[] = [$this->table.'.created_at', '<=', $end_date];

        $res = DB::connection($this->connection)
            ->table($this->table)
            ->where($where)
            ->leftJoin('user', $this->table.'.user_id', '=', 'user.user_id')
            ->leftJoin('prop', $this->table.'.prop_id', '=', 'prop.prop_id')
            ->select('nickname', 'uuid', 'prop.prop_id', 'name', 'change', $this->table.'.created_at', 'type')
            ->orderBy('created_at', 'desc')
            ->paginate($limit);
        $res = $this->objToArray($res);
        foreach ($res['data'] as $k => $v) {
            $res['data'][$k]['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
            $propNum = DB::connection($this->connection)->table('prop_user_pack')
                ->where('prop_id', '=', $v['prop_id'])
                ->select('num')
                ->first();
            $res['data'][$k]['num'] = $propNum->num;
        }

        return $res;
    }
}
